﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;

namespace XDocumentFormat
{
    public partial class XDocumentFormatForm : Form
    {
        public XDocumentFormatForm()
        {
            InitializeComponent();
        }

        private string fileName;

        private void openButton_Click(object sender, EventArgs e)
        {
            DialogResult result;

            using (OpenFileDialog fileChooser = new OpenFileDialog())
            {
                fileChooser.Filter = "XML Files (*.xml)|*.xml";
                fileChooser.FilterIndex = 0;
                fileChooser.DefaultExt = "xml";

                result = fileChooser.ShowDialog();
                fileName = fileChooser.FileName;
            }

            if (result == DialogResult.OK)
            {
                if (fileName == string.Empty)
                {
                    MessageBox.Show("Error reading file", "File Error", MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Error);
                }
                else if (Path.GetExtension(fileName) != ".xml")
                {
                    MessageBox.Show("Please choose a different file with .xml extension", "Wrong file extension",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {                                              
                        openTextBox.Text = fileName;
                        FormatXML();
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show(ex.Message, "File Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void FormatXML()
        {
            int identLevel = 0;

            XDocument xmlFile = XDocument.Load(fileName);
            PrintElement(xmlFile.Root, identLevel);
        }

        private void PrintElement(XElement element, int identLevel)
        {
            string elementName = element.Name.LocalName;

            OutputElementSpace(identLevel);
            outputTextBox.AppendText("<" + elementName);

            foreach (var attribute in element.Attributes())
            {
                outputTextBox.AppendText(" " + attribute.Name + "=" + attribute.Value);
            }

            outputTextBox.AppendText(">\r\n");


            if (element.HasElements)
            {
                foreach (XElement child in element.Elements())
                {
                    PrintElement(child, identLevel + 1);
                }
            }
            else
            {
                OutputElementSpace(identLevel + 1);

                outputTextBox.AppendText(element.Value.Trim() + "\n");
            }

            OutputElementSpace(identLevel);
            outputTextBox.AppendText(string.Format("</{0}>\n", elementName));
        }

        private void OutputElementSpace(int number)
        {
            for (int i = 0; i < number; i++)
            {
                outputTextBox.AppendText("    ");
            }
        }
    }
}